package com.hitqz.robot.util;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;

import java.util.Map;
import java.util.Optional;
import java.util.concurrent.TimeUnit;

public class ChipCacheUtil {

    public static final String KEY_AIR_QUALITY = "airQuality";
    public static final String KEY_JACK_DISTANCE = "jackDistance";


    private static final Cache<String, Boolean> stopCache = CacheBuilder.newBuilder()
            .maximumSize(1)
            .expireAfterWrite(2, TimeUnit.SECONDS) // 缓存过期时间
            .build();

    private static final Cache<String, Map<String, Object>> cache = CacheBuilder.newBuilder()
            .maximumSize(50)// fixme 缓存最大容量,按key实际种类去扩容
            .expireAfterWrite(5, TimeUnit.SECONDS) // 缓存过期时间
            .build();


    public static void setStopFlag(){
        stopCache.put("stop",true);
    }

    public static boolean getStopFlag(){
        //不存在的话就返回false
        return Optional.ofNullable(stopCache.getIfPresent("stop")).orElse(false);
    }

    // 使用命令 号做 key
    public static void put(String key, Map<String, Object> value) {
        cache.put(key, value);
    }

    public static Object get(String key) {
        return cache.getIfPresent(key);
    }

}
